﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using DAO;
using System.Web.Services;
using System.Web.Security;
using System.Drawing;

namespace TuyenDungViecLam.NguoiTimViec
{
    public partial class NguoiTimViecDangKy : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                DrlTinhThanh.DataSource = ChiTietCongViec_BUS.DanhSachNoiLamViec();
                DrlTinhThanh.DataTextField = "Ten";
                DrlTinhThanh.DataValueField = "MaTinhThanh";
                DrlTinhThanh.DataBind();

                //gioi tinh
                ListItem item1 = new ListItem("Nam", "true");
                ListItem item2 = new ListItem("Nữ", "false");
                DrlGioiTinh.Items.Add(item1);
                DrlGioiTinh.Items.Add(item2);

                //ngay sinh
                for (int i = 1; i <= 31; i++)
                {
                    DrlNgay.Items.Add(i.ToString());
                }

                for (int i = 1; i <= 12; i++)
                {
                    DrlThang.Items.Add(i.ToString());
                }
            }
            

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            try
            {
                NguoiTimViecDTO nguoiTimViec = new NguoiTimViecDTO();
                nguoiTimViec.UserName = txtUsername.Text;
                nguoiTimViec.PassWord = txtPassword.Text;
                nguoiTimViec.Date = DateTime.Now.Date;
                nguoiTimViec.Ten = txtTen.Text;
                nguoiTimViec.Ho = txtHo.Text;
                nguoiTimViec.GioiTinh = bool.Parse(DrlGioiTinh.SelectedItem.Value.ToString());
                nguoiTimViec.Email = txtEmail.Text;
                nguoiTimViec.NgaySinh = DateTime.Parse(DrlThang.SelectedValue.ToString() + "/" + DrlNgay.SelectedValue.ToString() + "/" + TxtNam.Text);

                nguoiTimViec.DienThoai = txtdienthoai.Text;
                nguoiTimViec.Didong = txtdidong.Text;
                nguoiTimViec.NgayDangKy = DateTime.Now.Date;
                nguoiTimViec.DiaChi = txtDiachi.Text;
                nguoiTimViec.MaTinhThanh = int.Parse(DrlTinhThanh.SelectedItem.Value.ToString());

                if(IsUserAvailable(txtUsername.Text))
                {
                    if (txtCaptcha.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase))
                    {

                        bool kq = NguoiTimViec_BUS.Register(nguoiTimViec);

                        if (kq == true)
                        {
                            divCapnhat.Visible = false;
                            divThongbao.Visible = true;
                            lbThongbao.Text = "Đăng kí thành công";
                            lbThongbao.ForeColor = Color.Blue;
                        }
                        else
                        {
                            divThongbao.Visible = true;
                            lbThongbao.Text = "Đăng kí thất bại";
                            lbThongbao.ForeColor = Color.Red;
                        }
                        lblMessage.Text = "";
                    }
                    else
                        lblMessage.Text = "Vui lòng nhập đúng chuỗi xác nhận";
                }
                else
                {
                    lblMessage.Text = "Username này đã tồn tại";
                }

            }
            catch (Exception)
            {
                lblMessage.Text = "chuỗi capcha có lỗi";
            }
            
        }

        [WebMethod]
        public static bool IsUserAvailable(string username)
        {
            MyMembershipProvider myMembership = new MyMembershipProvider();

            bool kq = myMembership.GetUser(username);

            if (kq == true)
            {
                return false;
            }
            else
            {
                return true;
            }

            
        }
    }

       
}
